#!/usr/bin/env perl 
#===============================================================================
#
#         FILE: showDir.pl
#
#        USAGE: ./showDir.pl  
#
#  DESCRIPTION: 
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: YOUR NAME (), 
# ORGANIZATION: 
#      VERSION: 1.0
#      CREATED: 06/03/2013 07:42:38 AM
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;
use Getopt::Long;

my $defDirectory = '/home/bxue';

sub showFile {
	my $localDiry = shift @_;
	my @fileArray = ();
	if (not -d $localDiry){
		die "$localDiry is not a directory!\n";
	}

	opendir my $dir, $localDiry or die "Can not open $localDiry : $!";
	foreach my $file (readdir $dir){
		if($file !~ /^\./){
			push @fileArray, $file;
		}
	}
	closedir $dir;
	
	my $totalFile = $#fileArray + 1;
	print "Total: $totalFile\n";

	@fileArray = sort @fileArray;
	for(my $cnt = 0; $cnt <= $#fileArray; $cnt++){
		if(($cnt % 4 == 0) and ($cnt != 0)){
			print "\n";
		}
		printf "%-30s", $fileArray[$cnt];
	}
	print "\n";
}

######################################################
# main
######################################################
my $listOpt;

unless(
	GetOptions(
		"list:s" =>	\$listOpt,
	)
){
		print "no unless!\n";
}
unless($listOpt){
	$listOpt = '.';
}
if(-x $listOpt){
	&showFile($listOpt);
}
else {
	print "showDir: cannot access $listOpt: No such file or directory\n";
}
